export default {
  // 处理获取短信验证码
  toggleValidCodeBtn (target, timeId) {

    if (timeId || target.valideCodeEnabled === false) {
      clearInterval(timeId)
      target.valideCodeEnabled = true
      return
    }

    target.valideCodeEnabled = false

    let max = 60
    target.validText = max + 's后可重发'

    timeId = setInterval(() => {
      max--

      if (max <= 0) {
        target.validText = target.validTextDefault
        target.valideCodeEnabled = true
        clearInterval(timeId)
      } else {
        target.validText = max + 's后可重发'
      }
    }, 1000)

    return timeId
  },

  saveUserInfo(info) {
    let user = this.get('USER') || {}
    user.token = info.token || ''
    user.coursenickname = info.coursenickname || ''
    this.save('USER', user, true)
  },

  // 保存用户Token信息
  saveUserToken (token) {
    let user = this.get('USER') || {}
    user.token = token
    this.save('USER', user, true)
  },

  // 获取用户Token信息
  getUserToken () {
    let user = this.get('USER') || {}
    return user.token || ''
  },

  checkUserLogined () {
    let logined = this.getUserToken()
    if (logined) {
      return true
    } else {
      return false
    }
  },

  clearUserInfo () {
    localStorage.clear()
    sessionStorage.clear()
  },

  minutesToDateString (minutes) {
    return this.secondsToDateString(minutes * 60)
  },

  secondsToDateString (second) {
    let time = second || 0

    if (time == 0) {
      return '00:00:00'
    }

    let seconds = 0
    let minutes = 0
    let hours = 0

    seconds = time % 60
    time = Math.floor(time / 60)

    if (time > 0) {
      minutes = time % 60
      hours = Math.floor(time / 60)
    } else {
      minutes = 0
      hours = 0
    }

    return (
      this.lpad(hours, 2, '0') +
      ':' +
      this.lpad(minutes, 2, '0') +
      ':' +
      this.lpad(seconds, 2, '0')
    )
  },

  lpad (str, length, padStr) {
    let result = str == null ? '' : str + ''
    let paddingLength = length || 0
    let padText = padStr == null ? ' ' : padStr

    if (result.length >= paddingLength) {
      return result
    }

    let restLength = paddingLength - result.length

    for (let i = 0; i < restLength; i++) {
      result = padText + result
    }

    return result
  },

  save (key, value, isPermanent) {
    let itemResult = value == null ? {} : value
    if (key && !isPermanent) {
      sessionStorage.setItem(key, JSON.stringify(itemResult))
    } else if (key && isPermanent) {
      localStorage.setItem(key, JSON.stringify(itemResult))
    }
  },

  get (key) {
    if (!key) {
      return
    }

    let result = sessionStorage.getItem(key)

    if (result == null) {
      result = localStorage.getItem(key)
    }

    if (result != null && result != 'undefined') {
      return JSON.parse(result)
    } else {
      return null
    }
  },

  clear (key) {
    if (sessionStorage.getItem(key) != null) {
      sessionStorage.setItem(key, null)
    }

    if (localStorage.getItem(key) != null) {
      localStorage.setItem(key, null)
    }
  },
  isEmptyObject (e) {
    var t
    for (t in e) {
      return !1
    }
    return !0
  },
  // 从对象数组中提取符合条件的数组
  findFieldEq (key, value, list) {
    var array = []
    for (var item in list) {
      var flag = false
      for (var fieldName in list[item]) {
        if (fieldName === key) {
          if (list[item][fieldName] === value) {
            flag = true
          }
        }
      }
      if (flag) {
        array.push(list[item])
      }
    }
    return array
  }
}
